package com.fairy.sso.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.fairy.sso.annotation.FairyControllerExceptionLog;
import com.fairy.sso.config.FairyConfig;
import com.fairy.sso.core.ValidateCode;

@Controller
public class SSOController {

	private static Logger log = Logger.getLogger(SSOController.class);
	@Autowired(required=true)
	private FairyConfig fairyConfig;
	@RequestMapping ("/index.do")
	public ModelAndView index(HttpServletRequest request,Model model,String service)
	{
	    //ModelAndView mav = new ModelAndView(FairyConstants.JSON_VIEW_RETAIN_DEFINED_VIEW); 
		model.addAttribute("service", service);
	    ModelAndView mav = new ModelAndView("index");

	    return mav; 
	}
	
	@RequestMapping ("/img.do")
	@FairyControllerExceptionLog(description="获取ticket")
	public void img(HttpServletRequest request, HttpServletResponse response,Model model,RedirectAttributes attr,String ticket)
	{
		response.setContentType("image/jpeg");  
        //禁止图像缓存。  
        response.setHeader("Pragma", "no-cache");  
        response.setHeader("Cache-Control", "no-cache");  
        response.setDateHeader("Expires", 0);  
          
        HttpSession session = request.getSession();  
        
        ValidateCode vCode = new ValidateCode(120,40,5,100);  
        String code=vCode.getCode();
       
        session.setAttribute("sso-code", code);  
        log.info("生成的验证码:"+code);
        try {
			vCode.write(response.getOutputStream());
		} catch (IOException e) {

			e.printStackTrace();
		}  
	}
}
